home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 November: Tool Chest / Dev.CD Nov 96 TC / Dev.CD Nov 96 TC.toast / Sample Code / Snippets / Files / FileSharingOn / FileSharingIsOn.c next >
Encoding:
C/C++ Source or Header  |  1996-08-09  |  11.0 KB  |  527 lines  |  [TEXT/CWIE]

  1. /******************************************************************************************
  2. FileSharingIsOn.c
  3.         
  4. This snippet demonstrates how to determine whether or not File Sharing is on.
  5.     
  6. Written by Virginia (Ginny) McCulloh
  7. Apple Developer Technical Support
  8. August, 1996, Cupertino, CA
  9. Copyright 1996, Apple Computer, Inc.
  10.  
  11. This runs under Metrowerks CodeWarrior 8 with the Universal 
  12. Interfaces 2.1 from ETO #18.
  13. ******************************************************************************************/
  14.  
  15.  
  16. #include <StandardFile.h>
  17. #include "FileSharingIsOn.h"
  18.  
  19. /*****     Prototypes      *****/
  20. void        main(void);
  21. void         initApp(void);
  22. Boolean        checkGestaltFeatures(void);
  23. Boolean     installAEHandlers();
  24. void         setUpMenus(void);
  25. void         setUpWindow(void);
  26. void         setUpMenus(void);
  27. void         doHighLevel(EventRecord *eventRec);
  28. void         doMousedown(EventRecord *eventRec);
  29. void         doKey(EventRecord *eventRec);
  30. void        doUpdate(EventRecord *eventRec);
  31. void         dispatch(long menuResult);
  32. void         doAppleCmds (short theItem);
  33. void         doFileCmds (short theItem);
  34. void         doTestCmds (short theItem);
  35. void         theTest(void);
  36. Boolean     sharingIsOn(void);
  37. Boolean     volIsSharable(short vRefNum);
  38.  
  39.  
  40.  
  41. pascal OSErr    DoOpenAppAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon);
  42. pascal OSErr    DoOpenDocAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon);
  43. pascal OSErr    DoPrintDocAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon);
  44. pascal OSErr    DoQuitAppAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon);
  45.  
  46.  
  47. /*****  globals *****/
  48. Boolean                    gQuitTheApp = false;        /* true = quit program */
  49. /*****    main() *****/
  50.  
  51. void main()
  52. {
  53.     short            sleepTime  =  60;
  54.     EventRecord        myEvent;
  55.  
  56.     
  57.     initApp();                                /* call initialization routine */
  58.     setUpMenus();
  59.     do
  60.     {
  61.         if (WaitNextEvent(everyEvent, &myEvent, sleepTime, NIL))
  62.             switch (myEvent.what)
  63.             {
  64.                 case mouseDown:
  65.                     doMousedown(&myEvent);
  66.                     break;
  67.                     
  68.                 case keyDown:
  69.                 case autoKey:
  70.                     doKey(&myEvent);
  71.                     break;
  72.  
  73.                 case kHighLevelEvent:
  74.                     doHighLevel(&myEvent);
  75.                     break;
  76.  
  77.                 case activateEvt:
  78.                     break;
  79.                 
  80.                 case updateEvt:
  81.                     doUpdate(&myEvent);
  82.                     break; 
  83.                 
  84.                 case diskEvt:
  85.                     break;
  86.             
  87.                 case osEvt:                /*  I don't really care yet. */
  88.                     break;
  89.             }
  90.     }  while (!gQuitTheApp);        
  91.     
  92. }                 /* end of main */
  93.  
  94.  
  95. /*****    initApp() *****/
  96.  
  97. void initApp()
  98. {
  99.     
  100.     /* Memory specific initializations. */
  101.     
  102.     MaxApplZone();                /* grow the heap to its maximum size */
  103.     MoreMasters();                   /* create more master pointers         */
  104.     MoreMasters();
  105.     MoreMasters();
  106.     MoreMasters();
  107.     MoreMasters();
  108.  
  109.     /* Initializing the ROM Managers */
  110.     
  111.     InitGraf((Ptr) &qd.thePort);
  112.     InitFonts();
  113.     InitWindows();
  114.     InitMenus();
  115.     FlushEvents(everyEvent,0);
  116.     TEInit();
  117.     InitDialogs(NIL);
  118.     InitCursor();
  119.     
  120.     if (!checkGestaltFeatures())
  121.         ExitToShell();
  122.     
  123.     /* Install the required Apple Event Handlers */
  124.     if (!installAEHandlers())
  125.         ExitToShell();
  126. }                /* end of initApp() */
  127.  
  128.  
  129.  
  130. /*****  checkGestaltFeatures() *****/
  131. Boolean checkGestaltFeatures()
  132. {
  133.     long        gestaltFeature;
  134.     OSErr        myErr;
  135.  
  136.     myErr = Gestalt(gestaltSystemVersion, &gestaltFeature);     /* which SysVersion present? */
  137.     if (myErr == noErr)
  138.     {
  139.         gestaltFeature = (gestaltFeature >> 8) & 0xf; 
  140.                                         /* shift result over & mask out major version number */
  141.         if (gestaltFeature < 7)         /* This is a System 7+ shell.  We quit otherwise. */
  142.         {
  143.             StopAlert(BADSYSTEMID, nil);
  144.             return(false);
  145.         }
  146.     }
  147.     
  148.     if (myErr == noErr)
  149.     {
  150.         myErr = Gestalt(gestaltQuickdrawVersion, &gestaltFeature);
  151.                                                     /* we want color QD cuz we're spoiled */
  152.         if (myErr == noErr)
  153.         {
  154.             if(gestaltFeature < gestalt32BitQD)
  155.             {
  156.                 StopAlert(BADQUICKDRAWID, nil);
  157.                 return(false);
  158.             }
  159.         }
  160.     }
  161.     
  162.     
  163.     
  164.     if (myErr == noErr)
  165.     {
  166.         myErr = Gestalt(gestaltAppleEventsAttr, &gestaltFeature);
  167.         if (myErr == noErr)
  168.         {
  169.             if (!(gestaltFeature & 0xf))
  170.             {
  171.                 StopAlert(NOAPPLEEVENTS, nil);
  172.                 return(false);
  173.             }
  174.         }
  175.     }
  176.     if (!myErr)        
  177.         return(true);            /* if there was an error we cannot continue */
  178.     else
  179.     {
  180.         return(false);            /* we made it through without a hitch */
  181.     }
  182. }
  183.  
  184. /*****    installAEHandlers *****/
  185. Boolean installAEHandlers()
  186. {
  187.     OSErr        myErr;
  188.     
  189.     myErr = AEInstallEventHandler ( kCoreEventClass, 
  190.                     kAEOpenApplication, NewAEEventHandlerProc(DoOpenAppAE), 0L, false );
  191.     if (myErr == noErr)
  192.         myErr = AEInstallEventHandler ( kCoreEventClass,
  193.                     kAEOpenDocuments, NewAEEventHandlerProc(DoOpenDocAE), 0L, false );
  194.     if (myErr == noErr)
  195.         myErr = AEInstallEventHandler ( kCoreEventClass,
  196.                     kAEPrintDocuments, NewAEEventHandlerProc(DoPrintDocAE), 0L, false );
  197.     if (myErr == noErr)
  198.         myErr = AEInstallEventHandler ( kCoreEventClass,
  199.                     kAEQuitApplication, NewAEEventHandlerProc(DoQuitAppAE), 0L, false );
  200.     if (myErr)
  201.         return(false);
  202.     else
  203.         return(true);
  204. }
  205.  
  206. /*****    setUpMenus() 
  207.             This will put up our menus.  Be sure to read the comments by the
  208.             AppendMenu() and SetItem() calls to see how different options
  209.             behave.        
  210. *****/
  211. void setUpMenus()
  212. {
  213.     short            n;
  214.     MenuHandle        theMenu;
  215.     
  216.     for (n = 0; n < MAXMENUS; n++)
  217.     {
  218.         theMenu = GetMenu(FIRSTMENUID + n);
  219.         if (theMenu)                  
  220.             InsertMenu(theMenu, 0);
  221.     }
  222.  
  223.     theMenu = GetMHandle(APPLEMENU);
  224.     if (theMenu)
  225.         AddResMenu(theMenu, 'DRVR');
  226.     
  227.     DrawMenuBar();
  228.     return;
  229. }                    /* end of setUpMenus()  */
  230.  
  231. /*****    setUpWindow() *****/
  232.  
  233. void setUpWindow( void )
  234. {
  235.     WindowPtr    myWind;
  236.  
  237.     myWind = GetNewCWindow(WINDOWID, nil, PUTINFRONT);
  238.     if (myWind != nil)
  239.     {
  240.         ShowWindow(myWind);
  241.     }
  242. }
  243.  
  244.  
  245. /*****    doMousedown()
  246.             We figure out where the user has clicked the mouse.  If the user
  247.             clicks anywhere but the menu bar, we beep.  Otherwise, we figure
  248.             out which menu they have clicked and dispatch.
  249. *****/
  250. void doMousedown(EventRecord *eventRec)
  251. {
  252.     short        windPart;
  253.     WindowPtr    myWind;
  254.     long        menuResult;
  255.     
  256.     windPart = FindWindow(eventRec->where, &myWind);
  257.     
  258.     switch(windPart)
  259.     {
  260.         case inContent:
  261.             SelectWindow(myWind);
  262.             break;
  263.  
  264.         case inDrag:
  265.             DragWindow( myWind, eventRec->where, &qd.screenBits.bounds );
  266.             break;
  267.  
  268.         case inMenuBar:
  269.             menuResult = MenuSelect(eventRec->where);
  270.             dispatch(menuResult);
  271.             break;
  272.  
  273.         case inSysWindow:
  274.             SystemClick( eventRec, myWind );
  275.             break;
  276.             
  277.         case inGoAway:
  278.             gQuitTheApp = true;
  279.             break;
  280.             
  281.         default:
  282.             break;
  283.     }
  284.     return;
  285. }                 /* end of doMousedown */
  286.  
  287.  
  288. /*****     doKey()
  289.             We ignore keys pressed unless they are accompanied by a 
  290.             command key.  Then we dispatch.
  291. *****/
  292. void doKey(EventRecord *eventRec)
  293. {
  294.     char    keyPressed;
  295.     long    menuResult;
  296.     
  297.     keyPressed = (char) (eventRec->message & charCodeMask);
  298.     
  299.     if((eventRec->modifiers & cmdKey) != 0)
  300.     {
  301.         menuResult = MenuKey(keyPressed);
  302.         dispatch(menuResult);    
  303.     }
  304.     return;
  305. }                 /* end of do_key */
  306.  
  307.  
  308. /*****    doHighLevel() *****/
  309. void doHighLevel(EventRecord *eventRec)
  310. {
  311.     OSErr myErr;
  312.     myErr = AEProcessAppleEvent(eventRec);
  313. }
  314.  
  315.  
  316. pascal OSErr    DoOpenAppAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon)
  317. {
  318.     #pragma unused(theAppleEvent, reply, refCon)
  319.     
  320.     setUpWindow();
  321.     return(noErr);
  322. }
  323. pascal OSErr    DoOpenDocAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon)
  324. {
  325.     #pragma unused(theAppleEvent, reply, refCon)
  326.     
  327.     return(noErr);
  328. }
  329. pascal OSErr    DoPrintDocAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon)
  330. {
  331.     #pragma unused(theAppleEvent, reply, refCon)
  332.     
  333.     return(noErr);
  334. }
  335. pascal OSErr    DoQuitAppAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon)
  336. {
  337.     #pragma unused(theAppleEvent, reply, refCon)
  338.     
  339.     gQuitTheApp = true;
  340.     return(noErr);
  341. }
  342.  
  343.  
  344. /***** doUpdate() *****/
  345. void    doUpdate(EventRecord *eventRec)
  346. {
  347.     GrafPtr        savedPort;
  348.     WindowPtr    theWindow;
  349.     theWindow = (WindowPtr) eventRec->message;
  350.     
  351.     GetPort(&savedPort);
  352.     SetPort((GrafPtr) theWindow);
  353.     BeginUpdate(theWindow);
  354.     EndUpdate(theWindow);
  355.     SetPort(savedPort);
  356. }
  357.  
  358.  
  359. /*****     dispatch()
  360.             We determine which menu the user has chosen (either with mouse
  361.             or with command keys) and jump to the routine that handles
  362.             that menu's commands.
  363. *****/
  364. void dispatch(long menuResult)
  365. {
  366.     short        theMenu;            /* menu selected */
  367.     short        theItem;            /* item selected */
  368.     
  369.     theMenu = HiWord (menuResult);        /* menuID selected */
  370.     theItem = LoWord (menuResult);        /* item# selected */
  371.     
  372.     switch (theMenu)
  373.     {
  374.         case APPLEMENU:
  375.             doAppleCmds(theItem);
  376.             break;
  377.  
  378.         case FILEMENU:
  379.             doFileCmds(theItem);
  380.             break;
  381.             
  382.         case EDITMENU:
  383.             break;
  384.  
  385.         case TESTMENU:
  386.             doTestCmds(theItem);
  387.             break;
  388.     }
  389.     HiliteMenu(0);
  390. }                /* end of dispatch */
  391.  
  392.  
  393.  
  394. /*****     doAppleCmds() 
  395.             When the user chooses the "About MyMenuText" item, we display the
  396.             About box.  If the user chooses a DA, we open the DA.
  397. *****/
  398. void doAppleCmds(short theItem)
  399. {    
  400.     MenuHandle        myMenu;
  401.     Str255            name;
  402.     short            dummy;
  403.     
  404.     if(theItem == appleABOUT)
  405.     {
  406.         Alert(ABOUTID, (ModalFilterUPP) NIL);
  407.     }
  408.     else
  409.     {
  410.         myMenu = GetMHandle (APPLEMENU);
  411.         if (myMenu)
  412.         {
  413.             GetItem(myMenu, theItem, name);
  414.             dummy = OpenDeskAcc(name);
  415.         }
  416.     }
  417.     return;
  418. }                 /* end of doAppleCmds */
  419.  
  420.  
  421.  
  422. /*****    doFileCmds() 
  423.             When the user chooses Quit on the File menu, we quit.
  424. *****/
  425. void doFileCmds (short theItem)
  426. {
  427.     switch (theItem)
  428.     {            
  429.         case fileQUIT:
  430.             gQuitTheApp = true;
  431.             break;
  432.     }
  433.     return;
  434. } /* end of doFileCmds */
  435.  
  436.  
  437.  
  438. /*****    doTestCmds()
  439.             When the user chooses the item in the Test menu, they
  440.             will get to choose a folder with a custom icon
  441. *****/
  442. void doTestCmds (short theItem)
  443. {    
  444.     
  445.     switch (theItem)
  446.     {
  447.         case testDOTHETEST:
  448.             theTest();
  449.             break;            
  450.     }    
  451.     return;
  452. }                /* end of doTestCmds */
  453.  
  454.  
  455. void theTest()
  456. {
  457.     Boolean        sharingOn;
  458.     
  459.     sharingOn = sharingIsOn();
  460.  
  461.     if (sharingOn)
  462.         DebugStr("\pSharing is on.");
  463. }
  464.  
  465.  
  466. /*
  467.     This is a C version of code in Inside Mac:Files for determining
  468.     if FileSharing is active on a particular Macintosh.
  469.     sharingIsOn indexes through all the local volumes on
  470.     a Macintosh with PBHGetVInfo(), so we can obtain true volume
  471.     reference number information.  We can take the vRefNum we obtain
  472.     and pass it to the volIsSharable() routine until we find a volume
  473.     for which FileSharing is truly active.  If we get an error, for
  474.     example, if we run out of volumes and get an error -35 (nsvErr), 
  475.     or if we find that FileSharing is active on a volume, then we drop
  476.     out of our while loop and return the results.
  477. */
  478. Boolean sharingIsOn()
  479. {
  480.     Boolean            sharing = false;
  481.     HParamBlockRec    myHPB;
  482.     OSErr            myErr;
  483.     short            volIndex = 1;
  484.     
  485.     do
  486.     {
  487.         myHPB.volumeParam.ioNamePtr = nil;
  488.         myHPB.volumeParam.ioVolIndex = volIndex;
  489.         
  490.         myErr = PBHGetVInfoSync(&myHPB);
  491.         if (myErr == noErr)
  492.             sharing = volIsSharable(myHPB.volumeParam.ioVRefNum);
  493.         volIndex++;
  494.     } while ((myErr == noErr) && (!sharing));
  495.     
  496.     return(sharing);
  497. }
  498.  
  499. /*
  500.     volIsSharable lets us know if the bHasPersonalAccessPrivileges bit
  501.     of the vMAttrib field of the GetVolParmsInfoBuffer is set, which 
  502.     indicates that FileSharing is active.
  503. */
  504. Boolean volIsSharable(short vRefNum)
  505. {
  506.     HParamBlockRec            myHPB;
  507.     GetVolParmsInfoBuffer    myInfoBuffer;
  508.     OSErr                    myErr;
  509.     
  510.     myHPB.ioParam.ioNamePtr = nil;
  511.     myHPB.ioParam.ioVRefNum = vRefNum;
  512.     myHPB.ioParam.ioBuffer = (Ptr) &myInfoBuffer;
  513.     myHPB.ioParam.ioReqCount = sizeof(GetVolParmsInfoBuffer);
  514.     
  515.  
  516.     myErr = PBHGetVolParmsSync(&myHPB);
  517.  
  518.     if (myErr == noErr)
  519.     {
  520.         if (((myInfoBuffer.vMAttrib) & (1L << bHasPersonalAccessPrivileges)) != 0)
  521.             return(true);
  522.         else
  523.             return(false);
  524.     }
  525.     else
  526.         return(false);
  527. }